package com.letv.whatslive.model.redis.chat.protocol;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zoran on 15-7-8.
 */
public class ChatEvent implements Serializable {

    /**
     * 协议版本：
     * 初始值是 101
     * 2016-01-18版本修改为230
     * 2016-08-29版本修改为276
     */
    private int version;
    private String uid; // 用户ID
    private String roomId; // 直播ID
    /**
     * 0=系统参数类消息，1=聊天消息，2=用户点赞，3=直播结束，4=用户加入直播 5=用户退出直播,6=用户禁言,7=强制下线直播,8=双人直播消息,
     * 9=运维系统消息,10=直播中的动画效果（包括火箭动画）,11=用户行为计数，12=礼物动画，13=观看相关数据增加消息，14=主播直播前后台运行
     * 15=边看边买相关消息
     */
    private int action;
    /**
     * action=0:1=关闭直播的点赞消息，2=修改点赞和评论消息队列配置参数, 3=关闭直播的用户进出消息, 4=用户退出直播聊天室消息, 5=指定用户禁言消息,
     *          6=关闭某个直播的用户聊天消息, 7=指定用户取消禁言消息
     * action=1:0=系统文本消息，1=文本消息，2=语音消息内容，3=语音消息链接,voice字段保存链接地址
     * action=8:2=副主播接受邀请，3=副主播拒绝邀请，4=主播结束邀请，5=副主播推流成功，6=主播切换屏幕，7=副主播结束直播，8=主播结束副主播的直播
     * action=9:1=文本消息，2=语音消息内容，3=语音消息链接,voice字段保存链接地址，4=带链接的文本消息，链接放在<$和$>之间
     * action=10:1=火箭动画
     * action=11:1=用户观看人次+1
     * action=12:2=礼物动画，2016-6-6为了兼容老版本，改成2
     * action=13:1=观看人数、观看人次和点赞数增加消息
     * action=14:1=前台运行，0表示后台运行
     * action=15:0=主播取消推荐商品，1=主播推荐商品，2=观众前去购买消息
     */
    private int type;
    private String nickName;//用户昵称
    private String picture; //用户头像
    /**
     * 消息正文
     * action=1,type=1:文本聊天消息
     * action=1,type=2:语音消息，消息时长
     * action=8,type=9:主屏幕streamId
     * action=10,type=1:火箭消息的文案
     * action=0,type=6:0表示关闭，其他表示打开
     * action=12,type=2:礼物消息的json，参见Gift
     * action=13:1=观看人数(watchNum)、观看人次(watchCount)和点赞数(likeNum)增加消息，消息格式为{'watchNum':10000,'watchCount':10000,'likeNum':20000}，值为0则不修改
     * action=15:type=0或者1:参考WeidianUserItems定义，WeidianUserItems的json字符串格式,type=2：content客户端传给服务端的时候为空，服务端传给客户端的时候为“***用户正在去购买的路上”文案。
     */
    private String content;
    private String voice;
    private Long time;//ChatEvent发送的相对时间，用于回放中聊天室消息的处理，精确到毫秒
    private long date; //发送Unix时间戳，精确到毫秒

    public ChatEvent() {

    }

    public static ChatEvent createChatEvent(String uid, String roomId, int action) {
        ChatEvent event = new ChatEvent();
        event.setUid(uid);
        event.setRoomId(roomId);
        event.setAction(action);
        event.setPicture("");
        event.setNickName("");
        event.setContent("");
        event.setType(0);
        event.setVoice(null);
        event.setVersion(276);
        event.setDate(System.currentTimeMillis());
        return event;
    }

    public static ChatEvent createChatEvent(String uid, String roomId, int action,String picture,String nickName) {
        ChatEvent event = new ChatEvent();
        event.setUid(uid);
        event.setRoomId(roomId);
        event.setAction(action);
        event.setPicture(picture);
        event.setNickName(nickName);
        event.setContent("");
        event.setType(0);
        event.setVersion(276);
        event.setDate(System.currentTimeMillis());
        return event;
    }

    public static ChatEvent createChatEvent(String uid, String roomId, int action,int type,String nickName) {
        ChatEvent event = new ChatEvent();
        event.setUid(uid);
        event.setRoomId(roomId);
        event.setAction(action);
        event.setType(type);
        event.setNickName(nickName);
        event.setContent("");
        event.setVersion(276);
        event.setDate(System.currentTimeMillis());
        return event;
    }

    public ChatEvent(String json) {
        JSONObject jsonObject = JSON.parseObject(json);
        this.date = jsonObject.getLong("date");
        this.uid = jsonObject.getString("uid");
        this.roomId = jsonObject.getString("roomId");
        this.action = jsonObject.getInteger("action");
        this.type = jsonObject.getInteger("type");
        this.nickName = jsonObject.getString("nickName");
        this.picture = jsonObject.getString("picture");
        this.content = jsonObject.getString("content");
        this.voice = jsonObject.getString("voice");
        this.version = jsonObject.getInteger("version");
        this.time = jsonObject.getLong("time");
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public String toLogString(){
        StringBuffer sb = new StringBuffer();
        sb.append("ChatEvent{");
        sb.append("uid=").append(uid);
        sb.append(", roomId=").append(roomId);
        sb.append(", action=").append(action);
        sb.append(", type=").append(type);
        sb.append(", nickName='").append(nickName).append("'");
        sb.append(", picture='").append(picture).append("'");
        sb.append(", content='").append(content).append("'");
        sb.append(", version='").append(version).append("'");
        if (voice != null && voice.length() > 0) {
            sb.append(", voice size='").append(voice.length()).append("'");
        }else{
            sb.append(", voice='null'");
        }
        sb.append(", time='").append(time).append("'");
        sb.append(", date=").append(date).append("}");
        return sb.toString();
    }
    public String toDbString(){
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("uid", uid);
        data.put("action", action);
        data.put("type", type);
        data.put("content", content);
        data.put("voice", voice);
        data.put("date",date);
        return JSON.toJSONString(data);
    }
    public boolean recordEvent(){
        if(action==1||(action==12&&type==2)){
            return true;
        }else{
            return false;
        }
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getRoomId() {
        return roomId;
    }

    public void setRoomId(String roomId) {
        this.roomId = roomId;
    }

    public int getAction() {
        return action;
    }

    public void setAction(int action) {
        this.action = action;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public long getDate() {
        return date;
    }

    public void setDate(long date) {
        this.date = date;
    }

    public String getVoice() {
        return voice;
    }

    public void setVoice(String voice) {
        this.voice = voice;
    }


    public Long getTime() {
        return time;
    }

    public void setTime(Long time) {
        this.time = time;
    }
}
